package com.dc.system.controller;

import com.dc.common.aop.LogAop;
import com.dc.common.aop.LoginAop;
import com.dc.common.aop.ResubmitAop;
import com.dc.common.constants.ConstantsCommon;
import com.dc.common.controller.BaseController;
import com.dc.common.em.EnumOperationModule;
import com.dc.common.model.BaseModel;
import com.dc.system.dto.SysUserDTO;
import com.dc.system.model.SysUser;
import com.dc.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/user")
@Api(description = "用户相关接口")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "系统用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query")
    })
    @PostMapping("/login")//post请求   等价于   @RequestMapping(value = "/login",method = RequestMethod.POST)
    public BaseModel<SysUser> login(BaseModel baseModel, SysUser user) throws Exception {
        return userService.login(baseModel, user, request);
    }

    @LoginAop
    @ApiOperation(value = "系统用户退出登录")
    @PostMapping("/loginOut")
    public BaseModel loginOut(BaseModel baseModel) throws Exception {
        return userService.loginOut(baseModel, request);
    }

    @LoginAop
    @ResubmitAop
    @LogAop(module = EnumOperationModule.No11103, type = ConstantsCommon.FUN_ADD)
    @ApiOperation(value = "添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名字", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态0禁用1启用", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "关联的部门id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "关联的角色id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "防止表单重复提交的令牌", dataType = "string", paramType = "query")
    })
    @PostMapping("/addUser")
    public BaseModel addUser(BaseModel baseModel, SysUser user, int deptId, int roleId) throws Exception {
        return userService.addUser(baseModel, user, request, deptId, roleId);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11103, type = ConstantsCommon.FUN_UPDATE)
    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名字", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态0禁用1启用", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "关联的部门id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "关联的角色id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "version", value = "数据版本", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("/updateUser")
    public BaseModel updateUser(BaseModel baseModel, SysUser user, int deptId, int roleId) throws Exception {
        return userService.updateUser(baseModel, user, request, deptId, roleId);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11103, type = ConstantsCommon.FUN_SEARCH )
    @ApiOperation(value = "根据id查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户自增id", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("getUserById")
    public BaseModel<SysUser> getUserById(BaseModel baseModel, SysUser user) throws Exception {
        return userService.getUserById(baseModel, user);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11103, type = ConstantsCommon.FUN_DELETE )
    @ApiOperation(value = "删除单个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户自增id", required = true, dataType = "Integer", paramType = "query")
    })
    @PostMapping("delUser")
    public BaseModel<SysUserDTO> delUser(BaseModel baseModel, SysUser user) throws Exception {
        return userService.delUser(baseModel, user,request);
    }

    @LoginAop
    @LogAop(module = EnumOperationModule.No11103, type = ConstantsCommon.FUN_SEARCH )
    @ApiOperation(value = "列表/高级查询部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "行数", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "key", value = "关键字", dataType = "string", paramType = "query")
    })
    @PostMapping("getUsers")
    public BaseModel<List<SysUserDTO>> getUsers(BaseModel baseModel, SysUser user) throws Exception {
        return userService.getUsers(baseModel, user);
    }
}
